﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using CoyoEden.Models;
using System.Collections.Specialized;
using System.Text;
using CoyoEden;

public partial class widgets_AlbumList_widget : WidgetBase
{
	protected void Page_Load(object sender, EventArgs e)
	{

	}

	#region base implementation
	public override string Name
	{
		get { return "AlbumList"; }
	}

	public override bool IsEditable
	{
		get { return true; }
	}

	public override void LoadWidget()
	{
		LoadSettings();
		BuildLayout();
	}
	#endregion

	#region member variables
	private List<Album> Albums;//选中的相册
	#endregion

	#region helper methods
	private void LoadSettings()
	{
		StringDictionary settings = GetSettings();
		var albumsIDs ="";
		if (settings.ContainsKey("albumlist"))
		{
			albumsIDs =settings["albumlist"];
			Albums = new List<Album>();
			albumsIDs.Split('|').ToList().ForEach(albumid => {
				if (!string.IsNullOrEmpty(albumid)) {
					var item = Album.GetAlbum(albumid);
					if(item!=null)
						Albums.Add(item);
				}
			});
		}
	}
	private void BuildLayout() {
		if (Albums == null || Albums.Count == 0) {
			phLeft.Controls.Add(new LiteralControl("未选定任何相册！"));
			phRight.Controls.Add(new LiteralControl("为选定任何相册！"));
			return;
		}
		//left part
		BuildLeftPartLayout();
		BuildRightPartLayout();
	}

	private void BuildLeftPartLayout() {
		StringBuilder html = new StringBuilder();
		html.AppendFormat("<ul class=\"{0}\">","hotalbums");
		//albums
		Albums.ForEach(item => {
			html.AppendFormat("<li class=\"{0}\"><a href=\"{1}album/{2}.aspx\" title=\"\">{3}</a><span class=\"gray albumtip\">[共{4}张照片]</span></li>","",Utils.AbsoluteWebRoot,item.Id,item.Title,item.Pics.Count);
		});
		html.AppendFormat("</ul>");
		phLeft.Controls.Add(new LiteralControl(html.ToString()));
	}
	private void BuildRightPartLayout() {
		StringBuilder html = new StringBuilder();
		//album icon
		Albums.ForEach(item => {
			var icon=Album.NoIcon;
			if(item.Pics!=null&&item.Pics.Count>0){
				icon=item.Pics[0].Url;
			}
			icon=Utils.ConvertToAbsolute(icon).ToString();
			html.AppendFormat("<a href=\"{0}album/{1}.aspx\" title=\"\"><img src=\"{2}&suffix={3}\" alt=\"{4}\" class=\"{5}\" width=\"80\" height=\"80\"/></a>",Utils.AbsoluteWebRoot,item.Id.ToString(),icon,"80x80",item.Title,"photo");
		});
		phRight.Controls.Add(new LiteralControl(html.ToString()));
	}
	#endregion
}
